﻿//-----------------------------------------------------------------------
// <copyright file="FontsViewModel.cs" author="Artak Mkrtchyan">
//     Creative Commons - GNU General Public License (http://creativecommons.org/licenses/GPL/2.0/
// </copyright>
// <author>Artak Mkrtchyan</author>
// <email>mkArtak@gmail.com</email>
// <date>15/07/2012</date>
//-----------------------------------------------------------------------

using ConsoleHoster.Common.Model;
using ConsoleHoster.Common.Utilities;
using ConsoleHoster.Common.Utilities.Threading;
using ConsoleHoster.Common.ViewModel;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Threading;

namespace ConsoleHoster.ViewModel
{
	internal class FontsViewModel : ViewModelBase
	{
		private const string LoadingFontsStatus = "Loading fonts...";

		private readonly IFontFamiliesProvider fontsProvider;
		private readonly FontSettings currentFontSettings;

		private ObservableCollection<string> fontFamilies;
		private string selectedFont;
		private bool useBoldFont;

		public FontsViewModel(IDispatcher argDispatcher, FontSettings argCurrentFontSettings, ILogger argLogger)
			: this(argDispatcher, new FontFamiliesProvider(), argCurrentFontSettings, argLogger)
		{
		}

		internal FontsViewModel(IDispatcher argDispatcher, IFontFamiliesProvider argFontsProvider, FontSettings argCurrentFontSettings, ILogger argLogger)
			: base(argDispatcher, new ComponentLogger("FontsViewModel", argLogger))
		{
			if (argCurrentFontSettings != null)
			{
				this.SelectedFont = argCurrentFontSettings.FontFamily;
			}

			this.fontsProvider = argFontsProvider;
			this.currentFontSettings = argCurrentFontSettings;
		}

		protected override void InitializeRuntimeMode()
		{
			base.InitializeRuntimeMode();

			this.RunAsyncAction(() =>
			{
				IEnumerable<string> tmpFontFamilies = this.fontsProvider.GetAvailableFontFamilies().Select(item => item.Source).OrderBy(item => item);
				this.RunActionOnUIThread(() =>
				{
					this.RunActionSwitchingToBusyState(() =>
					{
						this.FontFamilies = new ObservableCollection<string>(tmpFontFamilies);
						this.UseBoldFont = this.currentFontSettings.UseBoldFonts;
					}, true, LoadingFontsStatus);
				}, true, DispatcherPriority.Background);
			}, true, LoadingFontsStatus, true);
		}

		public FontSettings GetSettings()
		{
			return new FontSettings(this.SelectedFont, this.UseBoldFont);
		}

		public ObservableCollection<string> FontFamilies
		{
			get
			{
				return this.fontFamilies;
			}
			private set
			{
				this.fontFamilies = value;
				this.NotifyPropertyChange("FontFamilies");
			}
		}

		public string SelectedFont
		{
			get
			{
				return this.selectedFont;
			}
			set
			{
				if (value != this.selectedFont)
				{
					this.selectedFont = value;
					this.NotifyPropertyChange("SelectedFont");
				}
			}
		}

		public bool UseBoldFont
		{
			get
			{
				return this.useBoldFont;
			}
			set
			{
				if (value != this.useBoldFont)
				{
					this.useBoldFont = value;
					this.NotifyPropertyChange("UseBoldFont");
				}
			}
		}
	}
}